package com.example.test_event_and_callback.impl;

import com.example.test_event_and_callback.infras.event.BaseEventDto;
import com.example.test_event_and_callback.infras.event.CallableEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Song Dan Yang
 * Description: </br>
 * date: 2023/11/21 17:57
 */
@Component
public class Listener1 {

//    @Resource(name = "serviceTaskPool")
    private ThreadPoolExecutor serviceTaskPool;

    @Async("serviceTaskPool")
    @EventListener(CallableEvent.class)
    public void listener1(CallableEvent<BaseEventDto> testEvent){
//        System.out.println(Thread.currentThread().getName());
//        try {
////            System.out.println(serviceTaskPool.getActiveCount());
//            System.out.println(Thread.currentThread().getName());
//            Thread.sleep(0);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        testEvent.setResult("listener1","listener1的数据");
    }

    @Async("serviceTaskPool")
    @EventListener(CallableEvent.class)
    public void listener2(CallableEvent<BaseEventDto> testEvent){
//        System.out.println(Thread.currentThread().getName());
//        try {
////            System.out.println(serviceTaskPool.getActiveCount());
//            System.out.println(Thread.currentThread().getName());
//            Thread.sleep(0);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        testEvent.setResult("listener2","listener2的数据");
    }

    @Async("serviceTaskPool")
    @EventListener(CallableEvent.class)
    public void listener3(CallableEvent<BaseEventDto> testEvent){
//        System.out.println(Thread.currentThread().getName());
//        try {
////            System.out.println(serviceTaskPool.getActiveCount());
//            System.out.println(Thread.currentThread().getName());
//            Thread.sleep(0);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        testEvent.setResult("listener3","listener3的数据");
    }

}
